Skip to content

Remove use of JSONEncoder for Error Reporting #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 19, 2020
Merged

Conversation

fabianfett
Copy link
Member

Motivation

  • We would like for AWSLambdaRuntime to not link Foundation. For this we need to remove the use of JSONEncoder for error reporting.

Changes

  • Directly create the bytes needed to report the error with utf8 encoding

var bytes = [UInt8]()
bytes.append(UInt8(ascii: "{"))
bytes.append(contentsOf: #""errorType":"# .utf8)
self.encodeString(self.errorType, bytes: &bytes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errorType is a const string with 2 possible values, we I think we may be able to get away with not encoding it. it can become an enum with a string raw value + big comment above to make sure in anyone adds a value in the future they are aware of it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can save the encoding time. on the other, if the errorType is just a plain string, the encoding is just one scan (O(n)) and one copy. I doubt that we would see a performance gain in anything bigger than nanoseconds, while adding developer complexity.

your choice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay I guess its safer this way if anyone adds an errorType in the future that has special characters although the likelihood of that is close to zero

return bytes
}

private func encodeString(_ string: String, bytes: inout [UInt8]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should go to Utils, or at least as a floating function in the file as we may find other use for it in the future beyond ErrorResponse

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Copy link
Contributor

@tomerd tomerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good a couple of small nits

@tomerd tomerd merged commit 2cd6bce into master Mar 19, 2020
@tomerd tomerd deleted the remove-jsonencoder branch March 19, 2020 03:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants